package blackberryApp;

//import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
//import java.io.OutputStreamWriter;
//import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
//import java.sql.Blob;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import blackberryAppBus.BBApp_InfoBusiness;
import blackberryAppCom.BBApp_Common;
import blackberryAppCom.BBApp_Exception;

/**
 * Servlet implementation class BBApp_cInfoScreen
 */
public class BBApp_cInfoScreen extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BBApp_cInfoScreen() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		BBApp_InfoBusiness lObjBBApp_InfoBusiness = new BBApp_InfoBusiness();
		
		int lIntHidMode = BBApp_Common.fnChkInteger(BBApp_Common.fnGetValue(req.getParameter("hidMode")));
		if (lIntHidMode == 0) {
			lIntHidMode = 1;
		}
		
		String strRecNo		= "1";
		if (req.getParameter("hidRecNo") != null) {
			strRecNo = BBApp_Common.fnGetValue(req.getParameter("hidRecNo"));
		}
		req.getSession().setAttribute("RecNo", strRecNo);
		
		String strMaxNo		= "0";
		if (req.getParameter("hidMaxNo") != null) {
			strMaxNo = BBApp_Common.fnGetValue(req.getParameter("hidMaxNo"));
		}
		req.getSession().setAttribute("MaxNo", strMaxNo);
		
		String strGetDetail		= "0";
		if (req.getParameter("hidGetDetail") != null) {
			strGetDetail = BBApp_Common.fnGetValue(req.getParameter("hidGetDetail"));
		}
		req.getSession().setAttribute("GetDetail", strGetDetail);
		
		String strInfoTitle		= "";
		if (req.getParameter("hidInfoTitle") != null) {
			strInfoTitle = BBApp_Common.fnGetValue(req.getParameter("hidInfoTitle"));
		}
		req.getSession().setAttribute("InfoTitle", strInfoTitle);
		
		switch (lIntHidMode) {
			case 5: // dari blackberry, retrieve image
				try {
					ServletOutputStream sos = resp.getOutputStream();
					resp.setContentType("image/gif");
					
					Connection lCnFetch		      = null;	
					PreparedStatement lPStmtFetch = null;		
					ResultSet lRsFetch		      = null;
					StringBuffer lSBquery 		  = new StringBuffer();
					Blob myBlob					  = null;
					
					lCnFetch = new BBApp_Common().OpenConnection(3);
					if(lCnFetch == null) { //if connection is not established
						throw new BBApp_Exception("NoDataBase Connection","1500");
					}
					lSBquery.append("select b.foto ");
					lSBquery.append("  from berita.t_berita a, berita.t_berita_foto b ");
					lSBquery.append(" where 1 = 1 ");
					lSBquery.append("   and a.berita_id = b.berita_id ");
					lSBquery.append("   and upper(b.jn_foto) != 'K' ");
					lSBquery.append("   and upper(trim(a.judul_berita)) = upper(trim('"+strInfoTitle+"')) ");			
					lSBquery.append("   and rownum = 1 ");
					
					BBApp_Common.fnWriteLog("BBApp_InfoBusiness","BBApp_InfoBusiness(fnRetrieveImage) : ","lSBquery.toString(): "+lSBquery.toString());
					
					lPStmtFetch = lCnFetch.prepareStatement(lSBquery.toString());
					lRsFetch = lPStmtFetch.executeQuery();
					
					while(lRsFetch.next()) {
						myBlob = lRsFetch.getBlob(1);
					}
					
					InputStream in = myBlob.getBinaryStream(); 
					byte[] buffer = new byte[(int)myBlob.length()];
					int n = 0;
					while ((n = in.read(buffer))>=0) {
						sos.write(buffer, 0, n);
					}
					in.close();
					sos.flush();
					
					try {
						if (lRsFetch!=null) {lRsFetch.close(); lRsFetch=null;}
						if (lPStmtFetch!=null) {lPStmtFetch.close(); lPStmtFetch=null;}
						if (lCnFetch!=null) {lCnFetch.close(); lCnFetch=null;}
						if (sos!=null) {sos.close(); sos=null;}
					}
					catch(Exception e) {
						BBApp_Common.fnWriteLog("BBApp_InfoBusiness", "BBApp_InfoBusiness(fnRetrieveImage) : ","Close Exception : "+e);
					}	
					
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(case 5) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
				
			case 4: // dari blackberry, info asuransi
				try {
					Hashtable htInfoPerusahaan = new Hashtable();
					String strJnsBerita = "3";
					htInfoPerusahaan = lObjBBApp_InfoBusiness.fnRetrieveInfo(strRecNo, strGetDetail, strInfoTitle, strJnsBerita);
					
					if (htInfoPerusahaan.size() > 0) {
						Hashtable htInfoPersh = (Hashtable) htInfoPerusahaan.get("Result");
						if (htInfoPersh.size() > 0) {
							req.getSession().setAttribute("INFO_ASU", htInfoPersh);
							strRecNo = (String) htInfoPerusahaan.get("RecNo");
							req.getSession().setAttribute("RecNo", strRecNo);
							strMaxNo = (String) htInfoPerusahaan.get("MaxNo");
							req.getSession().setAttribute("MaxNo", strMaxNo);
							strGetDetail = (String) htInfoPerusahaan.get("Detail");
							req.getSession().setAttribute("Detail", strGetDetail);
						}
					}
					
					XMLOutput(req, resp, lIntHidMode);
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(case 4) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
			
			case 3: // dari blackberry, testimonial
				try {
					Hashtable htTestimonial = new Hashtable();
					String strJnsBerita = "2";
					htTestimonial = lObjBBApp_InfoBusiness.fnRetrieveInfo(strRecNo, strGetDetail, strInfoTitle, strJnsBerita);
					
					if (htTestimonial.size() > 0) {
						Hashtable htTesti = (Hashtable) htTestimonial.get("Result");
						if (htTesti.size() > 0) {
							req.getSession().setAttribute("TESTIMONIAL", htTesti);
							strRecNo = (String) htTestimonial.get("RecNo");
							req.getSession().setAttribute("RecNo", strRecNo);
							strMaxNo = (String) htTestimonial.get("MaxNo");
							req.getSession().setAttribute("MaxNo", strMaxNo);
							strGetDetail = (String) htTestimonial.get("Detail");
							req.getSession().setAttribute("Detail", strGetDetail);
						}
					}
					
					XMLOutput(req, resp, lIntHidMode);
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(case 3) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
			
			case 2: // dari blackberry, info perusahaan
				try {
					Hashtable htInfoPerusahaan = new Hashtable();
					String strJnsBerita = "1";
					htInfoPerusahaan = lObjBBApp_InfoBusiness.fnRetrieveInfo(strRecNo, strGetDetail, strInfoTitle, strJnsBerita);
					
					if (htInfoPerusahaan.size() > 0) {
						Hashtable htInfoPersh = (Hashtable) htInfoPerusahaan.get("Result");
						if (htInfoPersh.size() > 0) {
							req.getSession().setAttribute("INFO_PERSH", htInfoPersh);
							strRecNo = (String) htInfoPerusahaan.get("RecNo");
							req.getSession().setAttribute("RecNo", strRecNo);
							strMaxNo = (String) htInfoPerusahaan.get("MaxNo");
							req.getSession().setAttribute("MaxNo", strMaxNo);
							strGetDetail = (String) htInfoPerusahaan.get("Detail");
							req.getSession().setAttribute("Detail", strGetDetail);
						}
					}
//					BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(case 2) : ","check point 1");
					XMLOutput(req, resp, lIntHidMode);
//					BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(case 2) : ","check point 2");
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(case 2) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
				
			case 1: // dari blackberry, info RS Provider
				try {
					String lStrHurufAwal = "a";
					if (req.getParameter("hidHurufAwal") != null) {
						lStrHurufAwal = BBApp_Common.fnGetValue(req.getParameter("hidHurufAwal"));
					}
					
					String lStrRecNo		= "";
					if (req.getParameter("hidRecNo") != null) {
						lStrRecNo = BBApp_Common.fnGetValue(req.getParameter("hidRecNo"));
					}
					
					String lStrMaxNo		= "";
					if (req.getParameter("hidMaxNo") != null) {
						lStrMaxNo = BBApp_Common.fnGetValue(req.getParameter("hidMaxNo"));
					}
					
					Hashtable htResult = new Hashtable();
					htResult = lObjBBApp_InfoBusiness.fnRetrieveRSProvider(lStrHurufAwal, lStrRecNo, lStrMaxNo);
					
					Hashtable htProvider = (Hashtable) htResult.get("Result");
					if (htProvider.size() > 0) {
						req.getSession().setAttribute("RS", htProvider);
						lStrRecNo = (String) htResult.get("RecNo");
						req.getSession().setAttribute("RecNo", lStrRecNo);
						lStrMaxNo = (String) htResult.get("MaxNo");
						req.getSession().setAttribute("MaxNo", lStrMaxNo);
						req.getSession().setAttribute("HurufAwal", lStrHurufAwal);
					}
					
					XMLOutput(req, resp, lIntHidMode);
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(case 1) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
		}
	}
	
	/**
	 * html output
	 */
	public void HTMLOutput(HttpServletRequest req, HttpServletResponse resp, int lIntHidMode) {
//		String strReturn = "";
		try {
			resp.setContentType("text/html");
			PrintWriter out = resp.getWriter();
			if (lIntHidMode != 1) {
				if (req.getSession().getAttribute("INFO_PERSH") != null) {
					String strRecNo = (String) req.getSession().getAttribute("RecNo");
					int intRecNo = Integer.parseInt(strRecNo);
					String strMaxNo = (String) req.getSession().getAttribute("MaxNo");
					int intMaxNo = Integer.parseInt(strMaxNo);
					String strDetail = (String) req.getSession().getAttribute("Detail");
					int intDetail = Integer.parseInt(strDetail);
					Hashtable htProsedurKlaim = (Hashtable) req.getSession().getAttribute("INFO_PERSH");
					int intProKlaim = htProsedurKlaim.size();
					
					out.println("<html>");
					out.println("<body>");
					out.println("<table>");
					for (int a=intRecNo; a<=intRecNo+(intProKlaim-1); a++) {
						String[] strArray = (String[]) htProsedurKlaim.get(String.valueOf(a));
						String strInfoId = BBApp_Common.fnGetValue(strArray[0]);
						String strInfoTitle = BBApp_Common.fnGetValue(strArray[1]);
						String strInfoBody = BBApp_Common.fnGetValue(strArray[2]);
						String strInfoEntryDate = BBApp_Common.fnGetValue(strArray[3]);
						String strContributor = BBApp_Common.fnGetValue(strArray[4]);
						String strRowNum = BBApp_Common.fnGetValue(strArray[5]);
						
						out.println("<tr>");
						out.println("	<td>" + strInfoTitle + "</td>");
						out.println("</tr>");
						out.println("<tr>");
						out.println("	<td>" + strInfoBody + "</td>");
						out.println("</tr>");
						out.println("<tr>");
						out.println("	<td>(" + strContributor + ", " + strInfoEntryDate + ")</td>");
						out.println("</tr>");
					}
					out.println("</table>");
					out.println("</body>");
					out.println("</html>");
					
					out.close();
					
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
//		return strReturn;
	}
	
	/**
	 * XMLOutput
	 */
	public void XMLOutput(HttpServletRequest req, HttpServletResponse resp, int lIntHidMode) {
		try {
			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
	        Document doc = docBuilder.newDocument();
	        
	        ServletOutputStream out = resp.getOutputStream();
	        resp.setContentType("text/xml");
	        
	        String strXMLString = createXmlTree(doc, req, resp, lIntHidMode);
	        InputStream in = new ByteArrayInputStream(strXMLString.getBytes());
	        byte[] buffer = strXMLString.getBytes();
	        int intSize = in.read(buffer);
	        
	        if (intSize != -1) {
		    	out.write(buffer, 0, intSize);
		    } 
	        in.close();
		    out.flush();
		}
		catch (Exception exp) {
			BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","Exception : "+exp);
		}
	}
	
	/**
	 * createXmlTree
	 */
	public String createXmlTree(Document doc, HttpServletRequest req, HttpServletResponse resp, int lIntHidMode) throws Exception {
		
		// create XML tree
//		lSBquery.append("	select z.*, rownum row_num from ( ");
//		lSBquery.append("		select a.hospital_name, a.hospital_address ");
//		lSBquery.append("		, a.hospital_telepon, a.hospital_fax, a.hospital_email ");
//		lSBquery.append("		, decode(a.provider, 1, 'Provider', 'Non Provider') status_provider ");
		
		Element eRoot = doc.createElement("GlobalInfo");
		doc.appendChild(eRoot);
		
//		BBApp_Common.fnWriteLog("BBApp_cInfoScreen","BBApp_cInfoScreen(createXmlTree) : ","lIntHidMode: >>" + lIntHidMode + "<<");
		
		if (lIntHidMode == 1) {
			// list rumah sakit
			if (req.getSession().getAttribute("RS") != null) {
				String strHurufAwal = (String) req.getSession().getAttribute("HurufAwal");
				String strMaxNo = (String) req.getSession().getAttribute("MaxNo");
				String strRecNo = (String) req.getSession().getAttribute("RecNo");
				int intRecNo = Integer.parseInt(strRecNo);
				Hashtable htRS = (Hashtable) req.getSession().getAttribute("RS");
				int intRS = htRS.size();
				for (int a=intRecNo; a<=intRecNo+(intRS-1); a++) {
					String[] strArray = (String[]) htRS.get(String.valueOf(a));
					String strNamaRS = BBApp_Common.fnGetValue(strArray[0]).replaceAll("\\p{Punct}|034|039", "");
					String strAlamatRS = BBApp_Common.fnGetValue(strArray[1]);
					String strTelpRS = BBApp_Common.fnGetValue(strArray[2]);
					String strFaxRS = BBApp_Common.fnGetValue(strArray[3]);
					String strEmailRS = BBApp_Common.fnGetValue(strArray[4]);
					String strStsProvider = BBApp_Common.fnGetValue(strArray[5]);
					String strRowNum = BBApp_Common.fnGetValue(strArray[6]);
					
					Element eRumahSakit = doc.createElement("InfoRumahSakit");
					eRoot.appendChild(eRumahSakit);
					
						Element eNamaRS = doc.createElement("NamaRS");
						eRumahSakit.appendChild(eNamaRS);
						Text txtNamaRS = doc.createTextNode(strNamaRS);
						eNamaRS.appendChild(txtNamaRS);
						
						Element eAlamatRS = doc.createElement("AlamatRS");
						eRumahSakit.appendChild(eAlamatRS);
						Text txtAlamatRS = doc.createTextNode(strAlamatRS);
						eAlamatRS.appendChild(txtAlamatRS);
						
						Element eTelpRS = doc.createElement("TelpRS");
						eRumahSakit.appendChild(eTelpRS);
						Text txtTelpRS = doc.createTextNode(strTelpRS);
						eTelpRS.appendChild(txtTelpRS);
						
						Element eFaxRS = doc.createElement("FaxRS");
						eRumahSakit.appendChild(eFaxRS);
						Text txtFaxRS = doc.createTextNode(strFaxRS);
						eFaxRS.appendChild(txtFaxRS);
						
						Element eEmailRS = doc.createElement("EmailRS");
						eRumahSakit.appendChild(eEmailRS);
						Text txtEmailRS = doc.createTextNode(strEmailRS);
						eEmailRS.appendChild(txtEmailRS);
						
						Element eStsProvider = doc.createElement("StsProvider");
						eRumahSakit.appendChild(eStsProvider);
						Text txtStsProvider = doc.createTextNode(strStsProvider);
						eStsProvider.appendChild(txtStsProvider);
						
						Element eHurufAwal = doc.createElement("HurufAwal");
						eRumahSakit.appendChild(eHurufAwal);
						Text txtHurufAwal = doc.createTextNode(strHurufAwal);
						eHurufAwal.appendChild(txtHurufAwal);
						
						Element eMaxNo = doc.createElement("MaxNo");
						eRumahSakit.appendChild(eMaxNo);
						Text txtMaxNo = doc.createTextNode(strMaxNo);
						eMaxNo.appendChild(txtMaxNo);
						
						Element eRecNo = doc.createElement("RecNo");
						eRumahSakit.appendChild(eRecNo);
						Text txtRecNo = doc.createTextNode(strRecNo);
						//Text txtRecNo = doc.createTextNode(strRowNum);
						eRecNo.appendChild(txtRecNo);
				}
			}
		} else if (lIntHidMode == 2) {
			// info perusahaan
			if (req.getSession().getAttribute("INFO_PERSH") != null) {
				
				String strRecNo = (String) req.getSession().getAttribute("RecNo");
				int intRecNo = Integer.parseInt(strRecNo);
				
				String strMaxNo = (String) req.getSession().getAttribute("MaxNo");
				int intMaxNo = Integer.parseInt(strMaxNo);
				
				String strDetail = (String) req.getSession().getAttribute("Detail");
				int intDetail = Integer.parseInt(strDetail);
				
				Hashtable htProsedurKlaim = (Hashtable) req.getSession().getAttribute("INFO_PERSH");
				int intProKlaim = htProsedurKlaim.size();
				
				for (int a=intRecNo; a<=intRecNo+(intProKlaim-1); a++) {
					String[] strArray = (String[]) htProsedurKlaim.get(String.valueOf(a));
					String strInfoId = BBApp_Common.fnGetValue(strArray[0]);
					String strInfoTitle = BBApp_Common.fnGetValue(strArray[1]);
					String strInfoBody = BBApp_Common.fnGetValue(strArray[2]);
					String strInfoEntryDate = BBApp_Common.fnGetValue(strArray[3]);
					String strContributor = BBApp_Common.fnGetValue(strArray[4]);
					String strRowNum = BBApp_Common.fnGetValue(strArray[5]);
					String strBuffer = "";
					
					if (intDetail == 1) {
						strBuffer = BBApp_Common.fnGetValue(strArray[6]);
					}
					
					Element eProsedurKlaim = doc.createElement("InfoPerusahaan");
					eRoot.appendChild(eProsedurKlaim);
					
						Element eInfoId = doc.createElement("InfoId");
						eProsedurKlaim.appendChild(eInfoId);
						Text txtInfoId = doc.createTextNode(strInfoId);
						eInfoId.appendChild(txtInfoId);
						
						Element eInfoTitle = doc.createElement("InfoTitle");
						eProsedurKlaim.appendChild(eInfoTitle);
						Text txtInfoTitle = doc.createTextNode(strInfoTitle);
						eInfoTitle.appendChild(txtInfoTitle);
						
						Element eDetail = doc.createElement("Detail");
						eProsedurKlaim.appendChild(eDetail);
						Text txtDetail = doc.createTextNode(strDetail);
						eDetail.appendChild(txtDetail);
						
						Element eFoto = doc.createElement("Foto");
						eProsedurKlaim.appendChild(eFoto);
						Text txtFoto = doc.createTextNode(strBuffer);
						eFoto.appendChild(txtFoto);
						
						Element eInfoBody = doc.createElement("InfoBody");
						eProsedurKlaim.appendChild(eInfoBody);
						Text txtInfoBody = doc.createTextNode(strInfoBody);
						eInfoBody.appendChild(txtInfoBody);
						
						Element eInfoEntryDate = doc.createElement("InfoEntryDate");
						eProsedurKlaim.appendChild(eInfoEntryDate);
						Text txtInfoEntryDate = doc.createTextNode(strInfoEntryDate);
						eInfoEntryDate.appendChild(txtInfoEntryDate);
						
						Element eInfoContributor = doc.createElement("InfoContributor");
						eProsedurKlaim.appendChild(eInfoContributor);
						Text txtInfoContributor = doc.createTextNode(strContributor);
						eInfoContributor.appendChild(txtInfoContributor);
						
						Element eMaxNo = doc.createElement("MaxNo");
						eProsedurKlaim.appendChild(eMaxNo);
						Text txtMaxNo = doc.createTextNode(strMaxNo);
						eMaxNo.appendChild(txtMaxNo);
						
						Element eRecNo = doc.createElement("RecNo");
						eProsedurKlaim.appendChild(eRecNo);
						Text txtRecNo = doc.createTextNode(strRecNo);
						eRecNo.appendChild(txtRecNo);
						
						
				}
			}
		} else if (lIntHidMode == 3) {
			// testimonial
			if (req.getSession().getAttribute("TESTIMONIAL") != null) {
				
				String strRecNo = (String) req.getSession().getAttribute("RecNo");
				int intRecNo = Integer.parseInt(strRecNo);
				
				String strMaxNo = (String) req.getSession().getAttribute("MaxNo");
				int intMaxNo = Integer.parseInt(strMaxNo);
				
				String strDetail = (String) req.getSession().getAttribute("Detail");
				int intDetail = Integer.parseInt(strDetail);
				
				Hashtable htProsedurKlaim = (Hashtable) req.getSession().getAttribute("TESTIMONIAL");
				int intProKlaim = htProsedurKlaim.size();
				
				for (int a=intRecNo; a<=intRecNo+(intProKlaim-1); a++) {
					String[] strArray = (String[]) htProsedurKlaim.get(String.valueOf(a));
					String strInfoId = BBApp_Common.fnGetValue(strArray[0]);
					String strInfoTitle = BBApp_Common.fnGetValue(strArray[1]);
					String strInfoBody = BBApp_Common.fnGetValue(strArray[2]);
					String strInfoEntryDate = BBApp_Common.fnGetValue(strArray[3]);
					String strContributor = BBApp_Common.fnGetValue(strArray[4]);
					String strRowNum = BBApp_Common.fnGetValue(strArray[5]);
					String strBuffer = "";
					
					if (intDetail == 1) {
						strBuffer = BBApp_Common.fnGetValue(strArray[6]);
					}
					
					Element eProsedurKlaim = doc.createElement("Testimonial");
					eRoot.appendChild(eProsedurKlaim);
					
						Element eInfoId = doc.createElement("InfoId");
						eProsedurKlaim.appendChild(eInfoId);
						Text txtInfoId = doc.createTextNode(strInfoId);
						eInfoId.appendChild(txtInfoId);
						
						Element eInfoTitle = doc.createElement("InfoTitle");
						eProsedurKlaim.appendChild(eInfoTitle);
						Text txtInfoTitle = doc.createTextNode(strInfoTitle);
						eInfoTitle.appendChild(txtInfoTitle);

						Element eDetail = doc.createElement("Detail");
						eProsedurKlaim.appendChild(eDetail);
						Text txtDetail = doc.createTextNode(strDetail);
						eDetail.appendChild(txtDetail);
						
						Element eFoto = doc.createElement("Foto");
						eProsedurKlaim.appendChild(eFoto);
						Text txtFoto = doc.createTextNode(strBuffer);
						eFoto.appendChild(txtFoto);
						
						Element eInfoBody = doc.createElement("InfoBody");
						eProsedurKlaim.appendChild(eInfoBody);
						Text txtInfoBody = doc.createTextNode(strInfoBody);
						eInfoBody.appendChild(txtInfoBody);
						
						Element eInfoEntryDate = doc.createElement("InfoEntryDate");
						eProsedurKlaim.appendChild(eInfoEntryDate);
						Text txtInfoEntryDate = doc.createTextNode(strInfoEntryDate);
						eInfoEntryDate.appendChild(txtInfoEntryDate);
						
						Element eInfoContributor = doc.createElement("InfoContributor");
						eProsedurKlaim.appendChild(eInfoContributor);
						Text txtInfoContributor = doc.createTextNode(strContributor);
						eInfoContributor.appendChild(txtInfoContributor);
						
						Element eMaxNo = doc.createElement("MaxNo");
						eProsedurKlaim.appendChild(eMaxNo);
						Text txtMaxNo = doc.createTextNode(strMaxNo);
						eMaxNo.appendChild(txtMaxNo);
						
						Element eRecNo = doc.createElement("RecNo");
						eProsedurKlaim.appendChild(eRecNo);
						Text txtRecNo = doc.createTextNode(strRecNo);
						eRecNo.appendChild(txtRecNo);
						
				}
			}
		} else if (lIntHidMode == 4) {
			// info asuransi
			if (req.getSession().getAttribute("INFO_ASU") != null) {
				
				String strRecNo = (String) req.getSession().getAttribute("RecNo");
				int intRecNo = Integer.parseInt(strRecNo);
				
				String strMaxNo = (String) req.getSession().getAttribute("MaxNo");
				int intMaxNo = Integer.parseInt(strMaxNo);
				
				String strDetail = (String) req.getSession().getAttribute("Detail");
				int intDetail = Integer.parseInt(strDetail);
				
				Hashtable htProsedurKlaim = (Hashtable) req.getSession().getAttribute("INFO_ASU");
				int intProKlaim = htProsedurKlaim.size();
				
				for (int a=intRecNo; a<=intRecNo+(intProKlaim-1); a++) {
					String[] strArray = (String[]) htProsedurKlaim.get(String.valueOf(a));
					String strInfoId = BBApp_Common.fnGetValue(strArray[0]);
					String strInfoTitle = BBApp_Common.fnGetValue(strArray[1]);
					String strInfoBody = BBApp_Common.fnGetValue(strArray[2]);
					String strInfoEntryDate = BBApp_Common.fnGetValue(strArray[3]);
					String strContributor = BBApp_Common.fnGetValue(strArray[4]);
					String strRowNum = BBApp_Common.fnGetValue(strArray[5]);
					String strBuffer = "";
					
					if (intDetail == 1) {
						strBuffer = BBApp_Common.fnGetValue(strArray[6]);
					}
					
					Element eProsedurKlaim = doc.createElement("InfoAsuransi");
					eRoot.appendChild(eProsedurKlaim);
					
						Element eInfoId = doc.createElement("InfoId");
						eProsedurKlaim.appendChild(eInfoId);
						Text txtInfoId = doc.createTextNode(strInfoId);
						eInfoId.appendChild(txtInfoId);
						
						Element eInfoTitle = doc.createElement("InfoTitle");
						eProsedurKlaim.appendChild(eInfoTitle);
						Text txtInfoTitle = doc.createTextNode(strInfoTitle);
						eInfoTitle.appendChild(txtInfoTitle);
						
						Element eDetail = doc.createElement("Detail");
						eProsedurKlaim.appendChild(eDetail);
						Text txtDetail = doc.createTextNode(strDetail);
						eDetail.appendChild(txtDetail);
						
						Element eFoto = doc.createElement("Foto");
						eProsedurKlaim.appendChild(eFoto);
						Text txtFoto = doc.createTextNode(strBuffer);
						eFoto.appendChild(txtFoto);
						
						Element eInfoBody = doc.createElement("InfoBody");
						eProsedurKlaim.appendChild(eInfoBody);
						Text txtInfoBody = doc.createTextNode(strInfoBody);
						eInfoBody.appendChild(txtInfoBody);
						
						Element eInfoEntryDate = doc.createElement("InfoEntryDate");
						eProsedurKlaim.appendChild(eInfoEntryDate);
						Text txtInfoEntryDate = doc.createTextNode(strInfoEntryDate);
						eInfoEntryDate.appendChild(txtInfoEntryDate);
						
						Element eInfoContributor = doc.createElement("InfoContributor");
						eProsedurKlaim.appendChild(eInfoContributor);
						Text txtInfoContributor = doc.createTextNode(strContributor);
						eInfoContributor.appendChild(txtInfoContributor);
						
						Element eMaxNo = doc.createElement("MaxNo");
						eProsedurKlaim.appendChild(eMaxNo);
						Text txtMaxNo = doc.createTextNode(strMaxNo);
						eMaxNo.appendChild(txtMaxNo);
						
						Element eRecNo = doc.createElement("RecNo");
						eProsedurKlaim.appendChild(eRecNo);
						Text txtRecNo = doc.createTextNode(strRecNo);
						eRecNo.appendChild(txtRecNo);
						
						
				}
			}
		}
				
		//TransformerFactory instance is used to create Transformer objects. 
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        
        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        // bikin file dan tulis isi nya 
        // untuk check-ing aja..
		File file = new File("c:/newINFOxml.xml");
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
		bw.write(xmlString);
		bw.flush();
		bw.close();
        
        return xmlString;
		
	}
}
